package trees.tree

import trees.settings.Settings

class MutableNode(var children:List[AbstractLeaf], var treeStart:Int,
                   var treeEnd:Int, var idStart:Int,
                   var idEnd:Int)extends AbstractNode(children,
                                                              treeStart,treeEnd,idStart,idEnd){

    var ids:List[Int]=getLeafs()
    var labels:List[String]=ids map ((a)=>IdFactory.getLabel(a))
    var size:Int=ids.size
  
    def addChild(child:AbstractLeaf):Unit = {
        children= (child :: children).sort(_.getMinId<_.getMinId)
    }

    def addChild(new_children:List[AbstractLeaf]):Unit = {
        children= (new_children ::: children).sort(_.getMinId<_.getMinId)
    }
  
    override def getLeafs():List[Int] = {
        return children.foldLeft(List[Int]()){ (childs:List[Int], n) => childs ::: n.getLeafs() }
    }

    override def equals(b:Any):Boolean = b match{
        case l:Leaf => false
        case mn:MutableNode =>
            var same:Boolean = (mn.children.size == this.children.size)
            var i:Int=0
            while(same){
                same= mn.children(i).equals(this.children(i))
                i += 1
            }
            same
        case in:ImmutableNode =>
            var same:Boolean = (in.children.size == this.children.size)
            var i:Int=0
            while(same){
                same= in.children(i).equals(this.children(i))
                i += 1
            }
            same
        case an:AbstractNode => false
        case _ => false
    }

    override def deepCopy():AbstractLeaf = {
        new MutableNode(children.map(_.deepCopy),treeStart,treeEnd,idStart,idEnd)
    }

    override def changeMutability():AbstractLeaf = {
        new ImmutableNode(children.map(_.changeMutability),treeStart,treeEnd,idStart,idEnd)
    }


    override def toString() = {
        var treeString = "("
        for (child <- children){
            treeString += child.toString +","
        }
        treeString.substring(0, treeString.length-1) + ")"
    }

    override def getMinId():Int=children(0).getMinId
}
